const mysql = require('mysql');

module.exports = (sql) => {
    //第一步：创建链接对象
    let  connection = mysql.createConnection({
        host:'localhost',
        port:'3306',
        user:'root',
        password:'root',
        database:'1805a'
    })
    //第二步:链接mysql
    connection.connect((error) => {
        if(error){
            console.log('连接失败')
        }else{
            console.log('连接成功')
        }
    })

    //第三步：mysql进行增删改查的交互  交互的过程是异步的

    return new Promise((resolve,reject) => {
        connection.query(sql,(error, results) => {
            if(error){
                reject(error);
            }else{
                resolve(results);
            }
            //第四步：结束

            connection.end();
        })
    })

    
}

